<?php

namespace callback;

use globalunit\utils\KeysUtil;
use QYS\Util\Formater;
use QYS\Protocol\Request;
use QYS\Protocol\Response;
use QYS\Socket\Callback\SwooleHttp as ZSwooleHttp;
use QYS\Socket\IClient;
use QYS\Core\Route as ZRoute;
use QYS\Log\Log;
use QYS\Util\Debug;
use QYS\Db\Redis;
use QYS\Db\Mongo;
use QYS\third\Crypto\XXTEA;
use QYS\Core\Config;
use QYS\Core\Factory as CFactory;
use \Yosymfony\Toml\Toml;
use Swoole\Coroutine as co;
use globalunit\utils\Config as Cnf;
use globalunit\utils\QueueHelper;
use QYS\Db\Mysql;
use globalunit\utils\GlobalUnit;

use globalunit\utils\UniqueMessageQueueWithDelay;

// $array = Toml::Parse('key = [1,2,3]');

// print_r($array);

function strToHex($string){
    $hex = '';
    for ($i=0; $i<strlen($string); $i++){
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return strToUpper($hex);
}


class SwooleHttp extends ZSwooleHttp
{
    protected  $serv;
    protected  $workerId;
    protected  $beanpid;
    protected  $taskpid;
    protected  $queuepid;



    public function onRequest($request, $response)
    {
        do{
            ZRoute::route($request,$response);
        }while(0);
    }

    public function onWorkerStop($server, $workerId)
    {
        if(isset($this->taskpid)){
            \swoole_process::kill($this->taskpid);
        }

        if(isset($this->queuepid)){
            \swoole_process::kill($this->queuepid);
        }

        if(isset($this->beanpid)){
            \swoole_process::kill($this->beanpid);
        }
    }

    public function hearbeat(){
        $mongoconfig = Config::get("mongo", array());
        foreach ($mongoconfig as $key => $value) {
            $instance = Mongo::getInstance($key);
            try {
                $instance->listDatabases();
            } catch (\Exception $e) {
            }
        }
    }

    public function onWorkerStart($server, $workerId)
    {

        $gamename = Config::get("gamename");
        $this->gamename = $gamename;

        $version = Config::get("version");
        $this->version = $version;

        $GLOBALS['GAMENAME'] = $gamename;
        $GLOBALS['VERSION'] = $version;

        GlobalUnit::$server  = $server;
        $GLOBALS['WORKERID'] = $workerId;

        $workercount=$server->setting['worker_num'];
        $GLOBALS['WORKERCOUNT'] = $workercount;

        $version=Config::get("version");
        $GLOBALS['VERSION'] = $version;
        $this->serv = $server;
        $this->workerId = $workerId;
        parent::onWorkerStart($server, $workerId);
    }

    //执行异步任务 (Task)
    public function onTask($server, $task) {
        //业务代码
        try {
            $params=array("taskId"=>$task->id);
            $cmd = $task->data;
            $handler=CFactory::getInstance("handler\\TaskHandler",$params);
            $r = $handler->handle($cmd);
            if($r){
                $task->finish($r);
            }
        }catch (\Exception $e){
            Log::var_dump(['task message' =>$e->getMessage()]);
        }
    }

    public function onFinish($server, $taskId, $data)
    {
        Log::var_dump("onFinish");
        Log::var_dump($data);
    }
}
